﻿import com.dinmax.flash.utilities.timer.*
import com.dinmax.flash.utilities.movieclip.*
import com.dinmax.flash.utilities.math.*
import com.dinmax.factorium.*

class FactoriumBeeKy
extends MovieClip
implements TimerListener {
	
	//CONSTANTES LOCAS
	private var INIT_POS_X:Number = 60;
	private var INIT_POS_Y:Number = 60;
	private var DISABLE_POS_X:Number = 30;
	private var DISABLE_POS_Y:Number = 30;
	private var SCALE_FACTOR:Number = 3;
	private var ROTATION_FACTOR:Number = 10;
	private var RANGE_ROTATION:Number = 2;
	private var MIN_DIF_X:Number = 10;
	private var MIN_SCALE:Number = 50;
	//
	
	private var nNewX:Number = 50;
	private var nNewY:Number = 50;
	private var nRotation:Number = 1;
	
	//todo el asunto del timer interno
	private var DELAY:Number = 3000;
	private var bEnabled:Boolean;
	private var timer:Timer;
	//
	
	//movies que están dibujados
	private var subjectButton:MovieClip;
	private var mechButton:MovieClip;
	private var sirenButton:MovieClip;
	private var rightBalloon:MovieClip;
	//
	
	function FactoriumBeeKy() {
		this.onEnterFrame = this.volar;
		this.bEnabled = true;
		this.timer = new Timer();
		this.timer.addListener(this);
		this.timer.setDuration(this.DELAY);
		this.timer.startTimer();
		this.init();
		return;
	}
	
	public function init():Void {
		FactoriumApp.getApp().setBeeKy(this);
		return;
	};
	
	private function volar():Void {
		
		var difX:Number = (this._x - this.nNewX)*-1;
		var difY:Number = (this._y - this.nNewY)*-1;
		
		if(difX > this.MIN_DIF_X){
			this._rotation = difX/this.ROTATION_FACTOR;
		} else {
			this._rotation += 0.1*this.nRotation;
			if(this._rotation > this.RANGE_ROTATION || this._rotation < this.RANGE_ROTATION*-1){
				this.nRotation = this.nRotation * -1;
			}
		}
		this._y += difY/5;
		this._x += difX/10;
		if (this._y >= 3) {
			this._xscale = 10+(this._y/3);
			this._yscale = 10+(this._y/3);
		}
		return;
	};
	
	private function checkRollOver():Boolean {
		return MovieClipUtils.isMouseOver(this, _root);
	};
	
	function onMouseDown():Void {
		if(this.bEnabled){
			this.timer.startTimer();
			this.nNewX = _root._xmouse;
			this.nNewY = _root._ymouse;
		};
		return;
	}
		
	public function setEnabled(isEnabled:Boolean):Void {
		this.bEnabled = isEnabled;
		if(!this.bEnabled){
			this.setNextPos(this.DISABLE_POS_X, this.DISABLE_POS_Y);
			this.buttonOut(this.rightBalloon);
		} else {
			this.setNextPos(this.INIT_POS_X, this.INIT_POS_Y);
			this.timer.startTimer();
		};
	};
	public function setNextPos(x:Number, y:Number):Void {
		this.nNewX = x;
		this.nNewY = y;
		return;
	};
	public function randNextPos():Void {
		//lo primero que hago es resetear el contador para que tome al timer actual como nueva referencia
		this.timer.resetTimer();
		//actualizo las nuevas coordenadas usando un mensaje interno que devuelve un numero al azar dentro de un rango determinado por los dos parámetros
		do {
			var nRandomX:Number = MathUtils.randomRange(0, Stage.width-this._width/2);
		} while(Math.abs(this.nNewX - nRandomX) < 300)
	
		do {
			var nRandomY:Number = MathUtils.randomRange(0, Stage.height-this._height/2);
		} while(Math.abs(this.nNewX - nRandomX) < 150)
		this.setNextPos(nRandomX, nRandomY);
		
		return;
	};
	
	public function buttonOver(clip:MovieClip):Void {
		if(this._xscale > this.MIN_SCALE){
			switch(clip){
				case this.subjectButton:
				this.rightBalloon._y = this._ymouse;
				this.rightBalloon.showMessage("objetivos");
				break;
				// aca estoy posecionando el globo con la y del mouse
				case this.mechButton:
				this.rightBalloon._y = this._ymouse;
				this.rightBalloon.showMessage("mecanica");
				break;
				
				case this.sirenButton:
				trace("FactoriumBeeKy.as | buttonOver | sirena loca");
				break;
			}
		}
		return;
	}
	public function buttonOut(clip:MovieClip):Void {
		if(this._xscale > 50){
			this.rightBalloon.rewind();
		}
		return;
	}
	public function buttonPress(clip:MovieClip):Void {
		switch(clip){
			case subjectButton:
			FactoriumApp.getApp().loadContent(FactoriumApp.OBJETIVO);
			break;
			
			case mechButton:
			FactoriumApp.getApp().loadContent(FactoriumApp.MECANICA);
			break;
			
			case sirenButton:
			//FactoriumApp.getApp().loadContent(FactoriumApp.INFO);
			break;
		}
		return;
	}
	
	public function timeOut():Void {
		if(this.bEnabled){
			if(!this.checkRollOver()){
				this.randNextPos();
				this.timer.startTimer();
			} else {
				this.timer.startTimer();
			}
		}
		return;
	};
		
} // end class